#import "HTTPProcessor.h"

@implementation HTTPProcessor
- (id) initWithHost:(NSString *)_host port:(NSString *)_port {
    if ((self == [super init]) != nil) {
		[_host retain];
		host = _host;
		[_port retain];
		port = _port;
	}
	return self;
}

- (void)dealloc {
    [host release];
	[port release];
}

- (void) requestGet:_id cell:(NSString *)cell {
	dolog("requestGet");
	[self request:[NSString stringWithFormat:@"get-%@", cell] delegate:_id];
}

- (void) requestSet:_id cell:(NSString *)cell value:(NSString *)value {
	dolog("requestSet %s", [[NSString stringWithFormat:@"set-%@-%@", cell, value] UTF8String]);
	[self request:[NSString stringWithFormat:@"set-%@-%@", cell, value] delegate:_id];
}

- (void) request:(NSString *)command delegate:(id)_id {
    dolog("request %s",[command UTF8String]);
	delegate = _id;
	[delegate retain];
    dolog("creating request");
	NSString *urlString = [NSString stringWithFormat:@"http://%@:%@/%@", host, port, command];
	dolog("url:%s",[urlString UTF8String]);
	NSString *url = [NSURL URLWithString:urlString];
    NSURLRequest *theRequest = [NSURLRequest 
					requestWithURL:url 
                    cachePolicy:NSURLRequestReloadIgnoringCacheData 
                    timeoutInterval:60.0]; 
    dolog("initating connection");
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if (theConnection) { 
        receivedData = [[NSMutableData data] retain]; 
		dolog("initiated");
    } else { 
        dolog("can't connect");
		[delegate error:[@"Connection error" stringByAppendingFormat:@": %@",host]];
    } 
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    dolog("did receive response");
	//[receivedData setLength:0];
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    dolog("did receive data %d", [data length]);
	[receivedData appendData:data];
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    dolog("error: %s", [[error localizedDescription] UTF8String]);
	[delegate error:[[error localizedDescription] stringByAppendingFormat:@": %@",host]];
    [connection release]; 
    [receivedData release];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    dolog("Succeeded! Received %d bytes of data", [receivedData length]); 
	NSString *data = [[NSString alloc] initWithBytes:[receivedData bytes] length:[receivedData length] encoding:NSUTF8StringEncoding];
	dolog("data:%s", [data UTF8String]);
	if ([[data substringToIndex:2] compare:@"OK"] == NSOrderedSame) {
		dolog("reply OK, calling delegate");
		[delegate reply:data];
		[delegate release];
			/*
			if ([value compare:@"1"] == NSOrderedSame) {
				[mainView set:YES];
			} else if ([value compare:@"0"] == NSOrderedSame) {
				[mainView set:NO];
			} else {
				[mainView log:[@"Bad cell value" stringByAppendingString:value]];
			}
			*/
	}
	
    [connection release]; 
    [receivedData release]; 
} 

@end
